package com.example.firstapp.activity;

import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.example.firstapp.R;
import com.example.firstapp.entity.Restaurant;

public class RestaurantDetailActivity extends TabActivity {

	private Restaurant restaurant;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_restaurant_detail);
		// Show the Up button in the action bar.
		setupActionBar();

		TabHost tabHost = getTabHost();

		// Tab for Photos
		TabSpec infospec = tabHost.newTabSpec("Information");
		// setting Title and Icon for the Tab
		infospec.setIndicator("Information");
		Intent photosIntent = new Intent(this, InformationActivity.class);
		infospec.setContent(photosIntent);

		// Tab for Songs
		TabSpec reviewspec = tabHost.newTabSpec("Review");
		reviewspec.setIndicator("Review");
		Intent songsIntent = new Intent(this, ReviewActivity.class);
		reviewspec.setContent(songsIntent);

		// Adding all TabSpec to TabHost
		tabHost.addTab(infospec);
		tabHost.addTab(reviewspec);

		// Set up information
		restaurant = ((MobileApplication) getApplication()).getRestaurant();
		TextView name = (TextView) findViewById(R.id.restaurant_name);
		name.setText(restaurant.getName());

		ImageView rate = (ImageView) findViewById(R.id.restautant_rating);
		rate.setImageResource(getStarIcon(restaurant.getAverageRating()));

		TextView location = (TextView) findViewById(R.id.restaurant_location);
		location.setText(restaurant.getAddress());

		TextView distance = (TextView) findViewById(R.id.restaurant_distance);
		distance.setText(restaurant.getDistance());
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
	}

	private int getStarIcon(int star) {
		int image;
		switch (star) {
		case 0:
			image = R.drawable.rate_0;
			break;
		case 1:
			image = R.drawable.rate_1;
			break;
		case 2:
			image = R.drawable.rate_2;
			break;
		case 3:
			image = R.drawable.rate_3;
			break;
		case 4:
			image = R.drawable.rate_4;
			break;
		default:
			image = R.drawable.rate_5;
			break;
		}
		return image;
	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	public void showMaps(View view) {
		Intent intent = new Intent(Intent.ACTION_VIEW,
				Uri.parse("http://maps.google.com/maps?f=d&daddr="
						+ restaurant.getLatitude() + ","
						+ restaurant.getLongitude()));
		intent.setComponent(new ComponentName("com.google.android.apps.maps",
				"com.google.android.maps.MapsActivity"));
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.restaurant_detail, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
